package com.fast.services.impl;

import com.fast.dao.SysMenuMapper;
import com.fast.entity.SysMenu;
import com.fast.services.SysMenuService;
import com.fast.services.SysUserService;
import com.fast.utils.bean.Constants;
import com.fast.utils.service.BaseServiceImpl;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
 * Created by xiehaitao on 2017/9/6.
 */
@Service
@AllArgsConstructor
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService {


    SysMenuMapper sysMenuMapper;

    SysUserService sysUserService;


    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if (userId == Constants.SYSTEM_ADMIN) {
            List<SysMenu> menuList = this.queryAll();
            permsList = new ArrayList<>(menuList.size());
            for (SysMenu menu : menuList) {
                permsList.add(menu.getPerms());
            }
        } else {
            permsList = sysUserService.queryAllPerms(userId);
        }

        //用户权限列表
//        Set<String> permsSet = new HashSet<String>();
        return permsList.parallelStream().filter(perms -> StringUtils.isNotBlank(perms)).map(perms -> perms.split(",")).flatMap(Arrays::stream).collect(toSet());
//        permsSet = permsList.parallelStream().filter(perms -> StringUtils.isNotBlank(perms)).map(perms -> perms.split(",")).collect( toSet());
//        for (String perms : permsList) {
//            if (StringUtils.isBlank(perms)) {
//                continue;
//            }
//            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
//        }
//         permsSet;
    }

    @Override
    public List getUserMenuList(Long userId) {
        //系统管理员，拥有最高权限
        if (userId == Constants.SYSTEM_ADMIN) {
            return super.queryAll();
        }
        return sysMenuMapper.getUserMenuList(userId);
    }
}
